package com.inspur.gs.sbssdl.database.rysj.rysj.front.loginuserinfo.voactions;

import com.alibaba.fastjson.JSON;
import com.inspur.edp.bff.spi.AbstractFSAction;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.sysmanager.api.data.org.GSPSysOrg;
import io.iec.edp.caf.sysmanager.api.data.position.Position;
import io.iec.edp.caf.sysmanager.api.data.user.User;
import io.iec.edp.caf.sysmanager.api.data.user.UserExtend;
import io.iec.edp.caf.sysmanager.api.manager.org.SysOrgManager;
import io.iec.edp.caf.sysmanager.api.manager.position.PositionManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserExtendManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserPosOrgManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserPositionManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author HYG
 * 通过用户登录编码查询用户全部信息
 */
public class GetLoginUserInfoVOAction extends AbstractFSAction<String> {

    private String userCode;

    public GetLoginUserInfoVOAction(String userCode) {
        this.userCode = userCode;
    }

    @Override
    public void execute() {
        // 获取平台用户管理API
        UserManager userManager = SpringBeanUtils.getBean(UserManager.class);
        // 获取平台组织管理API
        SysOrgManager sysOrgManager = SpringBeanUtils.getBean(SysOrgManager.class);
        // 获取平台用户组织岗位管理API
        UserPosOrgManager userPosOrgManager = SpringBeanUtils.getBean(UserPosOrgManager.class);
        // 获取平台岗位管理API
        PositionManager positionManager = SpringBeanUtils.getBean(PositionManager.class);
        // 获取用户岗位管理API
        UserPositionManager userPositionManager = SpringBeanUtils.getBean(UserPositionManager.class);
        //获取用户拓展信息
        UserExtendManager userExtendManager = SpringBeanUtils.getBean(UserExtendManager.class);

        String userId = userManager.getUserIdByCode(userCode);
        User user = userManager.getUser(userId);

        List<String> positionIds = userPositionManager.getPositionIdsByUserId(userId);
        List<Position> positions = new ArrayList<>();
        for (String positionId : positionIds) {
            Position position = positionManager.getPosition(positionId);
            positions.add(position);
        }
        UserExtend userExtend = userExtendManager.getExtendByUserId(userId);
        GSPSysOrg sysOrg = sysOrgManager.getSysOrg(user.getSysOrgId());


        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("user", user);
        resultMap.put("position", positions);
        resultMap.put("extend", userExtend);
        resultMap.put("org", sysOrg);

        setResult(JSON.toJSONString(resultMap));
    }
}
